/*     */ package com.palmshop.online.alipay.util.httpClient;
/*     */ 
/*     */ import java.io.IOException;
/*     */ import java.net.UnknownHostException;

/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpConnectionManager;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
/*     */ import org.apache.commons.httpclient.NameValuePair;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class HttpProtocolHandler
/*     */ {
/*  28 */   private static String DEFAULT_CHARSET = "GBK";
/*     */ 
/*  31 */   private int defaultConnectionTimeout = 8000;
/*     */ 
/*  34 */   private int defaultSoTimeout = 30000;
/*     */ 
/*  37 */   private int defaultIdleConnTimeout = 60000;
/*     */ 
/*  39 */   private int defaultMaxConnPerHost = 30;
/*     */ 
/*  41 */   private int defaultMaxTotalConn = 80;
/*     */   private static final long defaultHttpConnectionManagerTimeout = 3000L;
/*     */   private HttpConnectionManager connectionManager;
/*  51 */   private static HttpProtocolHandler httpProtocolHandler = new HttpProtocolHandler();
/*     */ 
/*     */   public static HttpProtocolHandler getInstance()
/*     */   {
/*  59 */     return httpProtocolHandler;
/*     */   }
/*     */ 
/*     */   private HttpProtocolHandler()
/*     */   {
/*  67 */     this.connectionManager = new MultiThreadedHttpConnectionManager();
/*  68 */     this.connectionManager.getParams().setDefaultMaxConnectionsPerHost(this.defaultMaxConnPerHost);
/*  69 */     this.connectionManager.getParams().setMaxTotalConnections(this.defaultMaxTotalConn);
/*     */ 
/*  71 */     IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread();
/*  72 */     ict.addConnectionManager(this.connectionManager);
/*  73 */     ict.setConnectionTimeout(this.defaultIdleConnTimeout);
/*     */ 
/*  75 */     ict.start();
/*     */   }
/*     */ 
/*     */   public HttpResponse execute(HttpRequest request)
/*     */   {
/*  85 */     HttpClient httpclient = new HttpClient(this.connectionManager);
/*     */ 
/*  88 */     int connectionTimeout = this.defaultConnectionTimeout;
/*  89 */     if (request.getConnectionTimeout() > 0) {
/*  90 */       connectionTimeout = request.getConnectionTimeout();
/*     */     }
/*  92 */     httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
/*     */ 
/*  95 */     int soTimeout = this.defaultSoTimeout;
/*  96 */     if (request.getTimeout() > 0) {
/*  97 */       soTimeout = request.getTimeout();
/*     */     }
/*  99 */     httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);
/*     */ 
/* 102 */     httpclient.getParams().setConnectionManagerTimeout(3000L);
/*     */ 
/* 104 */     String charset = request.getCharset();
/* 105 */     charset = charset == null ? DEFAULT_CHARSET : charset;
/* 106 */     HttpMethod method = null;
/*     */ 
/* 108 */     if (request.getMethod().equals("GET")) {
/* 109 */       method = new GetMethod(request.getUrl());
/* 110 */       method.getParams().setCredentialCharset(charset);
/*     */ 
/* 113 */       method.setQueryString(request.getQueryString());
/*     */     } else {
/* 115 */       method = new PostMethod(request.getUrl());
/* 116 */       ((PostMethod)method).addParameters(request.getParameters());
/* 117 */       method.addRequestHeader("Content-Type", 
/* 118 */         "application/x-www-form-urlencoded; text/html; charset=" + charset);
/*     */     }
/*     */ 
/* 123 */     method.addRequestHeader("User-Agent", "Mozilla/4.0");
/* 124 */     HttpResponse response = new HttpResponse();
/*     */     try
/*     */     {
/* 127 */       httpclient.executeMethod(method);
/* 128 */       if (request.getResultType().equals(HttpResultType.STRING))
/* 129 */         response.setStringResult(method.getResponseBodyAsString());
/* 130 */       else if (request.getResultType().equals(HttpResultType.BYTES)) {
/* 131 */         response.setByteResult(method.getResponseBody());
/*     */       }
/* 133 */       response.setResponseHeaders(method.getResponseHeaders());
/*     */     }
/*     */     catch (UnknownHostException localUnknownHostException)
/*     */     {
/*     */       return null;
/*     */     }
/*     */     catch (IOException localIOException)
/*     */     {
/*     */       return null;
/*     */     }
/*     */     catch (Exception localException)
/*     */     {
/*     */       return null;
/*     */     } finally {
/* 144 */       method.releaseConnection(); } method.releaseConnection();
/*     */ 
/* 146 */     return response;
/*     */   }
/*     */ 
/*     */   protected String toString(NameValuePair[] nameValues)
/*     */   {
/* 156 */     if ((nameValues == null) || (nameValues.length == 0)) {
/* 157 */       return "null";
/*     */     }
/*     */ 
/* 160 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 162 */     for (int i = 0; i < nameValues.length; i++) {
/* 163 */       NameValuePair nameValue = nameValues[i];
/*     */ 
/* 165 */       if (i == 0)
/* 166 */         buffer.append(nameValue.getName() + "=" + nameValue.getValue());
/*     */       else {
/* 168 */         buffer.append("&" + nameValue.getName() + "=" + nameValue.getValue());
/*     */       }
/*     */     }
/*     */ 
/* 172 */     return buffer.toString();
/*     */   }
/*     */ }

